package com.dong.mcd.control;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dong.mcd.model.MusicHis;
import com.dong.mcd.pub.BaseAction;
import com.dong.mcd.pub.Constants;
import com.dong.mcd.service.IMusicService;

@Controller
@RequestMapping("/music")
public class MusicActon extends BaseAction {

	@Autowired
	private IMusicService musicService;

	/**
	 * 查询所有
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/search")
	public void search(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, String> param = paramMap(request);
		String name = param.get("name");// 查询音乐名称的关键字
		String srcType = param.get("srcType");// 音乐类型
		String pageIndexStr = param.get("pageIndex");
		String pageSizeStr = param.get("pageSize");
		int pageIndex = 0, pageSize = Constants.PAGE_SIZE;
		Integer _srcType = null;
		try {
			try {
				_srcType = !StringUtils.isEmpty(srcType) ? Integer
						.parseInt(srcType) : null;
				pageIndex = !StringUtils.isEmpty(pageIndexStr) ? Integer
						.parseInt(pageIndexStr) : 0;
				pageSize = !StringUtils.isEmpty(pageSizeStr) ? Integer
						.parseInt(pageSizeStr) : Constants.PAGE_SIZE;
			} catch (NumberFormatException e) {
			}
			jsonCallback(response, request, musicService.search(name, _srcType,
					pageIndex, pageSize).getPageMap());
		} catch (Exception e) {
			callError(response, request, "查询音乐异常");
			e.printStackTrace();
		}
	}

	/**
	 * 存储音乐
	 */
	public void store(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, String> param = paramMap(request);
		MusicHis music;
		String id = param.get("id");
		String name = param.get("name");
		String ablumn = param.get("ablumn");
		String lrcId = param.get("lrcId");
		String srcType = param.get("srcType");
		Integer _srcType = Constants.Music.SRC_TYPE_QQ;
		try {
			music = new MusicHis();
			music.setId(id);
			music.setId(name);
			music.setAblumn(ablumn);
			music.setLrcId(lrcId);
			try {
				_srcType = !StringUtils.isEmpty(srcType) ? Integer
						.parseInt(srcType) : Constants.Music.SRC_TYPE_QQ;
			} catch (NumberFormatException e) {
			}
			music.setSrcType(_srcType);
			musicService.store(music);
		} catch (Exception e) {
			callError(response, request, "保存音乐失败!");
			e.printStackTrace();
		}
	}
	
	public void remove(HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		Map<String, String> param = paramMap(request);
		String ids = param.get("ids");
		try {
			String[] idAry = ids.split(",");
			musicService.removeByIds(idAry);
		} catch (Exception e) {
			callError(response, request, "删除音乐失败!");
			e.printStackTrace();
		}
	}

}
